const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');

process.env.NODE_ENV = 'development';

module.exports = {
  entry: path.resolve(__dirname, '../src/index.js'),
  output: {
    filename: 'js/bundle.js',
    path: path.resolve(__dirname, '../build')
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          /**使用mini-css-extract-plugin 将生成的css 字符串创建为一个css文件 */
          MiniCssExtractPlugin.loader,
          'css-loader',
          /**
           * 这里是能够帮助postcss 找到package.json 中的browserslist里面的配置，通过配置加载指定的css兼容性样式 
           * "browserslist": {
                开发环境  --->  必须在你的打包环境中设置 process.env.NODE_ENV = 'development' 否则还是默认找production ，你设置的mode 这个配置在这里不起作用
                "development": [
                  "last 1 chrome version",
                  "last 1 firefox version",
                  "last 1 safari version"
                ],
                生成环境
                "production": [
                  ">0.2%",
                  "not dead",
                  "not op_mini all"
                ]
              }
           */
          {
            loader: 'postcss-loader',
            /**修改postcss-loader 的参数 */
            options: {
              // ident: 'postcss',
              // /**postcss 要使用的插件 */
              // plugins: () => [
              //   /**postcss-preset-env 是为了让我们能够精确控制兼容某个浏览器的版本的 */
              //   require('postcss-preset-env'),
              // ]
              postcssOptions: {
                plugins: [
                  "postcss-preset-env"
                ]
              }
            }
          }
        ]
      },
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /\.(jpg|png|gif|jpeg)$/,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024
          }
        },
        generator: {
          filename: 'img/[name]_[hash:8].[ext]',
          publicPath: './'
        }
      },
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        exclude: /\.(html|js|css|jpg|png|jpeg|gif|less)$/,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024
          }
        },
        generator: {
          filename: 'others/[name]_[hash:8].[ext]',
          publicPath: './'
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../index.html')
    }),
    new CleanWebpackPlugin(),
    /**分离css为单独文件的插件 */
    new MiniCssExtractPlugin({
      filename: 'css/[name].css'
    }),
    /**压缩css */
    new OptimizeCssAssetsWebpackPlugin()
  ],
  mode: 'development',
  devServer: {
    contentBase: path.resolve(__dirname, '../build'),
    compress: true,
    port: 3000,
    open: true,
    host: 'localhost'
  }
}
